package com.zswl.bus;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.media.MediaScannerConnection;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zswl.bus.login.LoginActivity;

import java.io.File;
import java.io.FileOutputStream;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends BaseWebActivity {

    private RxPermissions permissions;

    public static void startMe(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }

    @Override
    public String getUrl() {
        return String.format(Api.LOGIN, SpUtil.getUserId());
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        permissions = new RxPermissions(MainActivity.this);

        mAgentWeb.getJsInterfaceHolder().addJavaObject("android", new Js());

    }

    class Js {
        @JavascriptInterface
        public void share(String title,String jieshao,String id){
            String url = Api.HOST+"workDetailStore?companyid="+id+"&userid="+SpUtil.getUserId();
            ShareUtils.showShare(MainActivity.this,title,jieshao,url);
        }

        @JavascriptInterface
        public void checkSaveAction(final String s) {
            Log.d("---------fff-------", s);
            permissions.request(Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<Boolean>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(Boolean aBoolean) {
                            if (aBoolean) {
                                String path=FileUtil.getSDCardPath()+File.separator+Environment.DIRECTORY_DCIM+File.separator + System.currentTimeMillis() + ".jpg";
                                boolean flag = GenerateImg(s, path);
                                if (flag) {
                                    MediaScannerConnection.scanFile(context, new String[]{path},null,null);
                                    Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();

//                                    runOnUiThread(new Runnable() {
//                                        @Override
//                                        public void run() {
//                                        }
//                                    });
                                }

                            }
                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }

        @JavascriptInterface
        public void jsCallOCDRIVER(String id) {
            myId = id;
            changeHeaderImg();
        }

        @JavascriptInterface
        public void jsCallClose() {
            LoginActivity.startMe(context);
            SpUtil.clearSP();
        }

    }

    //对字节数组Base64编码
    public static boolean GenerateImg(String str, String imgPath) {//生成图片
        boolean flag = true;
        //将字符串转换成Bitmap类型
        Bitmap bitmap = null;
        try {
            bitmap = QrCodeUtil.syncEncodeQRCode(str, 800, Color.BLACK, Color.WHITE);
            FileOutputStream stream = new FileOutputStream(new File(imgPath));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            stream.flush();
            stream.close();
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }

}
